#!coding=utf-8
from standard_function import *

def del_appdown_task(driver):
    #清空下载任务
    try:
       btn = WaitforElementById(driver,'com.sogou.androidtool:id/btn_icon',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()

    while True:
        try:
            tasks=WaitforElementsById(driver,"com.sogou.androidtool:id/item_body",30)
        except NoSuchElementException:
            break
        else:
            tasks[0].click()
            try:
                clear_btn=WaitforElementById(driver,'com.sogou.androidtool:id/download_item_control_delete',30)
            except NoSuchElementException:
                break
            else:
                clear_btn.click()

            try:
                right_btn=WaitforElementById(driver,'com.sogou.androidtool:id/btn_right',30)
            except NoSuchElementException:
                break
            else:
                right_btn.click()
            time.sleep(2)
    driver.back()

def clickappdown(driver):
    try:
        list_f=WaitforElementById(driver,'com.sogou.androidtool:id/activity_category_view_pager',30)
    except NoSuchElementException:
        raise NoSuchElementException

    try:
        down_btns=WaitforElementsByClass(list_f,'android.widget.RelativeLayout',30)
    except NoSuchElementException:
        raise  NoSuchElementException
    else:
        print len(down_btns)
        for item in down_btns:
            try:
                size=WaitforElementById(item,"com.sogou.androidtool:id/app_size",30).get_attribute("text")
            except NoSuchElementException:
                continue
            print size.split()[1].strip()
            if 'G' not in size:
                if 'K' in size or float(size.split()[1].strip()[0:-3])<10:
                    continue
            try:
                down_btn=WaitforElementById(item,'com.sogou.androidtool:id/app_download_button',5)
            except NoSuchElementException:
                continue
            down_btn.click()
            time.sleep(2)
            try:
                install_btn=WaitforElementById(driver,'com.android.packageinstaller:id/cancel_button',2)
            except NoSuchElementException:
                pass
            else:
                install_btn.click()
            try:
                cancel_btn=WaitforElementById(driver,'com.sogou.androidtool:id/cancel',2)
            except NoSuchElementException:
                pass
            else:
                cancel_btn.click()

            #down_btn.click()
            waittime=20
            while waittime:
                try:
                    text=WaitforElementById(item,'com.sogou.androidtool:id/download_size',30).get_attribute("text")
                except NoSuchElementException:
                    down_btn.click()
                    break
                else:
                    print text
                    if '0/' not in text:
                        down_btn.click()
                        break
                    else:
                        time.sleep(0.5)
                        if waittime==0:
                            down_btn.click()
                            break
                        waittime=waittime-1
            print "----------------------------------------------"
    #driver.back()



def soft(driver):
    categorys = [u'通讯聊天',u'系统输入',u'影音图像',u'主题壁纸',u'出行必备',u'购物优惠',u'生活娱乐',u'常用工具',u'阅读学习',u'商务办公',u'新闻资讯',u'金融理财',u'健康医疗',u'儿童亲子']
    try:
        WaitforElementByName(driver,u'应用',30).click()
        WaitforElementByName(driver,u"分类",30).click()
    except NoSuchElementException:
        raise  NoSuchElementException
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for i in categorys:
        print "Current category is:",i
        try:
            btn=WaitforElementByName(driver,i,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                WaitforElementByName(driver,u'全部',30).click()
            except NoSuchElementException:
                driver.back()
                continue
            try:
                clickappdown(driver)
            except NoSuchElementException:
                continue
            driver.back()

def game(driver):
    categorys = [u'休闲益智',u'网络游戏',u'卡牌游戏',u'消除游戏',u'动作跑酷',u'扑克棋牌',u'经营策略',u'飞行射击',u'角色扮演',u'体育竞速']
    try:
        WaitforElementByName(driver,u'游戏',30).click()
        WaitforElementByName(driver,u"分类",30).click()
    except NoSuchElementException:
        raise  NoSuchElementException
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for i in categorys:
        print "Current category is:",i
        try:
            btn=WaitforElementByName(driver,i,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                WaitforElementByName(driver,u'全部',30).click()
            except NoSuchElementException:
                driver.back()
                continue
            try:
                clickappdown(driver)
            except NoSuchElementException:
                continue
            driver.back()

def run():
    desired_caps = {}
    #desired_caps['automationName'] = 'uiautomator2'
    desired_caps['platformName'] = "Android"               # 声明是ios还是Android系统
    desired_caps['platformVersion'] = ANDROID_VERSION             # Android内核版本号，也可以在模拟器设置中查看
    desired_caps['deviceName'] = DEVICE        # 连接的设备名称，可通过adb devices查看
    desired_caps['appPackage'] = 'com.sogou.androidtool'    # apk的包名
    desired_caps['appActivity'] = '.SliderTabPagerActivity'          # apk的launcherActivity
    desired_caps['noReset']='true'                         #不需要再次安装apk
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)          # 建立 session
    time.sleep(3)
    #driver.find_element_by_name().tag_name
    del_appdown_task(driver)
    soft(driver)
    time.sleep(2)
    game(driver)
    driver.quit()

#run()